<%@ page contentType="text/html; charset=UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="static/jquery.min.js"></script>

</head>

<body>
	摇一摇
</body>
<script type="text/javascript"> 
	if (window.DeviceMotionEvent) {
	  window.addEventListener('devicemotion', deviceMotionHandler, false);
	} else {
	  document.getElementById("dmEvent").innerHTML = "设备不支持"
	}
	
	 
   var img1 = document.createElement("img"); 
   img1.style.width = "375px"; 
   img1.style.height = "560px";
	
	var SHAKE_THRESHOLD = 500;
	var last_update = 0;
	var x, y, z, last_x, last_y, last_z;       
	function deviceMotionHandler(eventData) {
	  var acceleration = eventData.accelerationIncludingGravity;
	  var acceleration = eventData.accelerationIncludingGravity;
	  var curTime = new Date().getTime();       
	  if ((curTime - last_update)> 300) {                
	      var diffTime = curTime -last_update;
	      last_update = curTime;       
	      x = acceleration.x;
	      y = acceleration.y;
	      z = acceleration.z;       
	      var speed = Math.abs(x +y + z - last_x - last_y - last_z) / diffTime * 10000;          
	           if (speed > SHAKE_THRESHOLD) {
	                //alert("中奖了!");
	                var ranN=Math.floor(Math.random()*7);
	                
	                img1.src = "img/"+ranN+".jpg"; 
	           } 
	      last_x = x;
	      last_y = y;
	      last_z = z;
	    }
	  }
	
	 document.body.appendChild(img1); 
 </script> 
</html>